﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace tsgl
{
    public partial class barcodeOutput : Form
    {
        public barcodeOutput(string a,string b)
        {
            InitializeComponent();
            this.txtStartBarcode.Text = a;
            this.nudNumber.Value = decimal.Parse(b);
        }


        private static string setBarcode(string barcode, int barcodeLength)
        {
            int length = barcodeLength - barcode.Trim().Length;
            if (length > 0)
            {
                for (int i = 1; i <= length; i++)
                {
                    barcode = "0";
                }
            }
            return barcode;
        }

        private void createAzBarCodeCtrl(int locationX,int locationY,int axWidth,int axHeight,string barcode,string barcodeType)
        {
            AxBARCODELib.AxBarCodeCtrl axBarCode=new AxBARCODELib.AxBarCodeCtrl();
            panel1.Controls.Add(axBarCode);
            axBarCode.Location = new System.Drawing.Point(locationX,locationY);
            axBarCode.Size = new System.Drawing.Size(axWidth,axHeight);
            axBarCode.BackColor = Color.Wheat;
            axBarCode.ForeColor = Color.Blue;
            axBarCode.Style =short.Parse(barcodeType);
            axBarCode.LineWeight = 2;
            axBarCode.Direction = 0;
            axBarCode.ShowData = 1;
            axBarCode.Value = barcode;
        }

        private void barcodeOutput_Load(object sender, EventArgs e)
        {
            this.cboBarcodeType.Items.Add("EAN8码");
            this.cboBarcodeType.Items.Add("ENA13码");
            this.cboBarcodeType.Items.Add("Code 39码");
            this.cboBarcodeSource.Items.Add("图书条码");
            this.cboBarcodeSource.Items.Add("借书证条码");
            this.cboBarcodeType.SelectedIndex = 0;
            this.cboBarcodeSource.SelectedIndex = 0;
            this.nudLength.Value = 8;
            this.nudWidth.Value = 140;
            this.nudHeight.Value = 50;
            this.nudSpace.Value = 5;
            this.nudCols.Value = 4;
            //if (this.txtStartBarcode.Text.Trim() != "" || this.nudNumber.Value != "")
            //{
               
            //}
        }

        private void numericUpDown2_ValueChanged(object sender, EventArgs e)
        {

        }

        private void btnbarcodeOutput_Click(object sender, EventArgs e)
        {
            string barcodeType=null;
            int locationY=0,locatioX=0,j;
            switch(this.cboBarcodeType.SelectedIndex)
            {
                case 0:
                    barcodeType = "3";
                    break;
                case 1:
                    barcodeType = "2";
                    break;
                case 2:
                    barcodeType = "6";
                    break;
            }
            string barcode = this.txtStartBarcode.Text.Trim();
            int barcodeHeight = Convert.ToInt32(this.nudHeight.Value);
            int barcodeWidth = Convert.ToInt32(this.nudWidth.Value);
            int barcodeSpaceX = Convert.ToInt32(this.nudSpace.Value);
            int barcodeSpaceY = barcodeSpaceX + 5;
            int barcodeLength = Convert.ToInt32(this.nudLength.Value);
            int numbers = Convert.ToInt32(this.nudNumber.Value);
            int colsMax = Convert.ToInt32(this.nudCols.Value);
            int rows = numbers / colsMax;
            int colsMin = numbers % colsMax;
            if (colsMin != 0)
            {
                rows = rows + 1;
            }
            for (int i=0; i <= rows - 1; i++)
            {
                if (i == 0)
                {
                    locationY = 10;
                }
                else
                {
                    locationY = locationY + barcodeSpaceY + barcodeHeight;
                }
                if (i == rows - 1 || colsMin != 0)
                {
                    for (j = 0; j <= colsMin - 1; j++)
                    {
                        if (j == 0)
                        {
                            locatioX = 10;
                        }
                        else
                        {
                            locatioX = locatioX + barcodeSpaceY + barcodeHeight;
                        }
                        barcode = setBarcode(barcode, barcodeLength);
                        createAzBarCodeCtrl(locatioX, locationY, barcodeWidth, barcodeHeight, barcode, barcodeType);
                        barcode = Convert.ToString(int.Parse(barcode) + 1);
                    }
                }
                else
                {
                    for (j = 0; j <= colsMax - 1; j++)
                    {
                        if (j == 0)
                        {
                            locatioX = 10;
                        }
                        else
                        {
                            locatioX = locatioX + barcodeSpaceX + barcodeWidth;
                        }
                        barcode = barcodeOutput.setBarcode(barcode,barcodeLength);
                        createAzBarCodeCtrl(locatioX, locationY, barcodeWidth, barcodeHeight, barcode, barcodeType);
                        barcode = Convert.ToString(int.Parse(barcode) + 1);
                    }
                }
            }
        }

        private void cboBarcodeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (this.cboBarcodeType.SelectedIndex)
            {
                case 0:
                    this.nudLength.Value = 8;
                    break;
                case 1:
                    this.nudLength.Value = 13;
                    break;
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }


    }
}